import requests
import time
import csv
import pandas as pd
import logging
from apscheduler.schedulers.blocking import BlockingScheduler
import re
from lxml import etree
import json


class sunnyportal(object):
    def __init__(self):
        self.url = 'https://www.sunnyportal.com/Plants/GetPlantList/'
        self.headers ={
            'Host': 'www.sunnyportal.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
            'Connection': 'keep-alive',
            'Content-Type':'application/x-www-form-urlencoded',
            'Referer':'https://www.sunnyportal.com/Templates/Start.aspx',
            'Upgrade-Insecure-Requests':'1',
            'Origin':'https://www.sunnyportal.com'
        }
        # self.postdata = open('postdata.txt','rb')
        self.postdata = {
            '__EVENTTARGET':'',
            '__EVENTARGUMENT':'',
            'ctl00$ContentPlaceHolder1$Logincontrol1$txtUserName':'monitoring@hokucorp.com',
            'ctl00$ContentPlaceHolder1$Logincontrol1$txtPassword':'Hokusolar1288',
            'ctl00$ContentPlaceHolder1$Logincontrol1$LoginBtn':'%E7%99%BB%E5%BD%95',
            'ctl00$ContentPlaceHolder1$Logincontrol1$RedirectURL':'',
            'ctl00$ContentPlaceHolder1$Logincontrol1$RedirectPlant':'',
            'ctl00$ContentPlaceHolder1$Logincontrol1$RedirectPage': '',
            'ctl00$ContentPlaceHolder1$Logincontrol1$RedirectDevice': '',
            'ctl00$ContentPlaceHolder1$Logincontrol1$RedirectOther': '',
            'ctl00$ContentPlaceHolder1$Logincontrol1$PlantIdentifier': '',
            'ctl00$ContentPlaceHolder1$Logincontrol1$ServiceAccess': 'true',
            'ClientScreenWidth': '1920',
            'ClientScreenHeight': '1080',
            'ClientScreenAvailWidth': '1920',
            'ClientScreenAvailHeight': '1024',
            'ClientWindowInnerWidth': '1920',
            'ClientWindowInnerHeight': '352',
            'ClientBrowserVersion': '3',
            'ctl00$ContentPlaceHolder1$hiddenLanguage': 'zh-cn',
        }
        self.content = {
            'SiteName':[],
            'PeakPower': [],
            'YieldToday': [],
            'YieldYesterday': [],
            'YieldTotal': [],
            'YieldMonth': [],
            'PerformanceMonth': [],
            'PerformanceYear': [],
            'CurrentPower': [],
            'TimeStamp': [],
            'Co2': [],
            'TodayProfit': [],
            'Weather': [],
        }
        self.content1 = {
            'SiteName':[],
            'PeakPower': [],
            'YieldToday': [],
            'YieldYesterday': [],
            'YieldTotal': [],
            'YieldMonth': [],
            'PerformanceMonth': [],
            'PerformanceYear': [],
            'CurrentPower': [],
            'TimeStamp': [],
            'Co2': [],
            'TodayProfit': [],
            'Weather': [],
        }

    def parse(self):
        getX = requests.get('https://www.sunnyportal.com/Templates/Start.aspx',headers = self.headers,)
        D = getX.cookies.get_dict()
        cookies = []
        cookiestr = ''
        for k, v in D.items():
            cookies.append(k + '=' + v)
        for i in range(len(cookies)):
            cookiestr += cookies[i] + ';'
        # print cookiestr
        self.headers['Cookie'] = cookiestr
        tree = etree.HTML(getX.text)
        VIEWSTATEGENERATOR = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0]
        VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0]
        self.postdata['__VIEWSTATE'] = VIEWSTATE
        self.postdata['__VIEWSTATEGENERATOR']=VIEWSTATEGENERATOR
        s = requests.Session()
        s.post('https://www.sunnyportal.com/Templates/Start.aspx',headers = self.headers,data=self.postdata,)
        time.sleep(5)
        r = s.get('https://www.sunnyportal.com/Plants/GetPlantList')
        for item in r.json()['aaData']:
            time.sleep(1)
            if item['PlantName'][:3] =='DOT':
                siteName = item['PlantName'].split(' ')[:3]
                siteName = siteName[0]+' '+siteName[1]+' '+siteName[2]
                self.content['SiteName'].append(siteName)
                self.content['PeakPower'].append(item['PeakPowerValue'])
                self.content['YieldTotal'].append(item['YieldTotalValue'])
                self.content['YieldYesterday'].append(item['YieldYesterdayValue'])
                self.content['YieldMonth'].append(item['YieldMonthValue'])
                self.content['PerformanceMonth'].append(item['PerformanceMonthValue'])
                self.content['PerformanceYear'].append(item['PerformanceYearValue'])
                r1 = s.get('https://www.sunnyportal.com/'+'RedirectToPlant/'+item['DT_RowId'])
                r2 = s.get('https://www.sunnyportal.com/Dashboard/Weather')
                tree = etree.HTML(r1.text)
                CurrentPower = tree.xpath('//*[@id="TabSwitchDeviceSelectionContent2"]/div[1]/div[3]/div/span[1]/@data-value')
                TimeStamp = tree.xpath('//*[@id="TabSwitchDeviceSelectionContent2"]/div[1]/div[3]/div/span[1]/@data-timestamp')
                YieldToday = tree.xpath('//*[@id="ctl00_ContentPlaceHolder1_UserControlShowDashboard1_energyYieldWidget_energyYieldValue"]/text()')
                TodayProfit = tree.xpath('//*[@id="ctl00_ContentPlaceHolder1_UserControlShowDashboard1_revenueWidget_revenuePeriodValue"]/text()')
                Co2 =tree.xpath('//*[@id="ctl00_ContentPlaceHolder1_UserControlShowDashboard1_carbonWidget_carbonReductionValue"]/text()')
                if r2.text:
                    try:
                        Weather = int(r2.json()['today']['Temperature'])
                    except:
                        Weather = ' '
                else:
                    Weather = ' '
                if CurrentPower:
                    self.content['YieldToday'].append(YieldToday[0])
                    self.content['CurrentPower'].append(CurrentPower[0])
                    self.content['TimeStamp'].append(TimeStamp[0])
                    self.content['TodayProfit'].append(TodayProfit[0])
                    self.content['Co2'].append(Co2[0])
                    self.content['Weather'].append(Weather)
                else:
                    self.content['YieldToday'].append(' ')
                    self.content['CurrentPower'].append(' ')
                    self.content['TimeStamp'].append(' ')
                    self.content['TodayProfit'].append(' ')
                    self.content['Co2'].append(' ')
                    self.content['Weather'].append(' ')
        s.close()
        # print r.json()['aaData']

    def writeCsv(self,data):
        currentTime = time.time()
        currentTime = time.localtime(currentTime)
        currentTime = time.strftime('%Y-%m-%d,%H-%M-%S', currentTime)
        df = pd.DataFrame(data=data,columns=data.keys())
        df.to_csv('F:/python/sunnyportal/sunnyportal-%s.csv'%str(currentTime),index=False)

    def start(self):
        begin = time.time()
        logging.basicConfig(filename='sunnyportal.log', filemode='w', level=logging.DEBUG)
        self.parse()
        self.writeCsv(data=self.content)
        self.content=self.content1
        end = time.time()
        logging.debug('consume time:%s'%str(end-begin))
        print '%.2f'%(end-begin)

if __name__ == "__main__":
    # while True:
    #     try:
    #         sunnyportal().start()
    #         time.sleep(3600)
    #     except BaseException,e:
    #         print 'this exception is:',e
    #         print 'cannot request'
    #         time.sleep(600)
    sunnyportal().start()

